package org.shanyu.tide.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

public class RedEnvelopUtil {

    // 发红包算法，金额参数以分为单位
    public static List<Integer> divideRedPackage(Integer totalAmount, Integer totalPeopleNum) {
        List<Integer> amountList = new ArrayList<Integer>();

        Integer restAmount = totalAmount;

        Integer restPeopleNum = totalPeopleNum;

        Random random = new Random();

        for (int i = 0; i < totalPeopleNum - 1; i++) {
            // 随机范围：[1，剩余人均金额的两倍)，左闭右开
            int amount = random.nextInt(restAmount / restPeopleNum * 2 - 1) + 1;
            restAmount -= amount;
            restPeopleNum--;
            amountList.add(amount);
        }
        amountList.add(restAmount);

        return amountList;
    }

    //
    public static List<Integer> lineDivideRedPackage(Integer totalAmount, Integer totalPeopleNum) {
        List<Integer> list = new ArrayList<Integer>();
        Random random = new Random();

        while (list.size()<=totalPeopleNum) {
            int i = random.nextInt(totalAmount)+1;//最低1分钱
            if(list.indexOf(i)<0){//非重复切割添加到集合
                list.add(i);
            }
        }

        Collections.sort(list);
        int  flag=0;
        int fl=0;
        for (int i=0;i<list.size();i++) {
            int temp=list.get(i)-flag;
            flag=list.get(i);
            fl+=temp;
            System.out.println("红包切割金额" + AmountUtil.div(temp,100));
        }
        //最后一个红包
        list.add(totalAmount - fl);

        return list;
    }

}
